const express = require('express')
const router = express.Router();
module.exports = router

const db = require('../db')
const { formatData } = require('../utils')

// GET /api/goods/soup   获取养生汤列表
router.get('/soup', async (req, res) => {

    // 数据库操作
    let sql = `select * from soup`
    const data = await db.query(sql)
    res.send(formatData({data}))
})

//POST /api/goods/soup        添加养生汤商品
router.post('/soup', async (req, res) => {

    const {goodsname,price,imgurl,qty=10} = req.body
    let sqls = [`insert into soup (goodsname,price,imgurl,qty) values (`]
    let sql = []
    sql.push (`'${goodsname}'`)
    sql.push (`'${price}'`)
    sql.push (`'${imgurl}'`)
    sql.push (`'${qty}'`)
    sqls = sqls + sql.join()+` )` 

    const data = await db.query(sqls)

    res.send(formatData({data}));
})

//DELETE /api/goods/soup/:id         删除养生汤商品
router.delete('/soup/:id', async(req, res)=>{
    const {id} = req.params
    let sql = `delete  from soup where id=${id}`
    const data = await db.query(sql)
    res.send(
        formatData({
            code: data.length<0 ? 200 : 400,
            data
        })
    )
})


// --------------------------------------------------------

// GET /api/goods/tom   获取冬阴功汤列表
router.get('/tom', async (req, res) => {

    // 数据库操作
    let sql = `select * from tom_yam`
    const data = await db.query(sql)
    res.send(formatData({data}))
})

//POST /api/goods/tom        添加冬阴功汤商品
router.post('/tom', async (req, res) => {

    const {goodsname,price,imgurl,qty=10} = req.body
    let sqls = [`insert into tom (goodsname,price,imgurl,qty) values (`]
    let sql = []
    sql.push (`'${goodsname}'`)
    sql.push (`'${price}'`)
    sql.push (`'${imgurl}'`)
    sql.push (`'${qty}'`)
    sqls = sqls + sql.join()+` )` 

    const data = await db.query(sqls)

    res.send(formatData({data}));
})


//DELETE /api/goods/tom/:id         删除冬阴功汤商品
router.delete('/tom/:id', async(req, res)=>{
    const {id} = req.params
    let sql = `delete  from tom_yam where id=${id}`
    const data = await db.query(sql)
    res.send(
        formatData({
            code: data.length<0 ? 200 : 400,
            data
        })
    )
})



// --------------------------------------------------------

// GET /api/goods/snack   获取小吃列表
router.get('/snack', async (req, res) => {

    // 数据库操作
    let sql = `select * from snack`
    const data = await db.query(sql)
    res.send(formatData({data}))
})

//POST /api/goods/snack        添加小吃商品
router.post('/snack', async (req, res) => {

    const {goodsname,price,imgurl,qty=10} = req.body
    let sqls = [`insert into snack (goodsname,price,imgurl,qty) values (`]
    let sql = []
    sql.push (`'${goodsname}'`)
    sql.push (`'${price}'`)
    sql.push (`'${imgurl}'`)
    sql.push (`'${qty}'`)
    sqls = sqls + sql.join()+` )` 

    const data = await db.query(sqls)

    res.send(formatData({data}));
})

//DELETE /api/goods/snack/:id         删除小吃商品
router.delete('/snack/:id', async(req, res)=>{
    const {id} = req.params
    let sql = `delete  from snack where id=${id}`
    const data = await db.query(sql)
    res.send(
        formatData({
            code: data.length<0 ? 200 : 400,
            data
        })
    )
})



// --------------------------------------------------------

// GET /api/goods/salad   获取沙拉列表
router.get('/salad', async (req, res) => {

    // 数据库操作
    let sql = `select * from salad`
    const data = await db.query(sql)
    res.send(formatData({data}))
})

//POST /api/goods/salad        添加沙拉商品
router.post('/salad', async (req, res) => {

    const {goodsname,price,imgurl,qty=10} = req.body
    let sqls = [`insert into salad (goodsname,price,imgurl,qty) values (`]
    let sql = []
    sql.push (`'${goodsname}'`)
    sql.push (`'${price}'`)
    sql.push (`'${imgurl}'`)
    sql.push (`'${qty}'`)
    sqls = sqls + sql.join()+` )` 

    const data = await db.query(sqls)

    res.send(formatData({data}));
})


//DELETE /api/goods/salad/:id         删除沙拉商品
router.delete('/salad/:id', async(req, res)=>{
    const {id} = req.params
    let sql = `delete  from salad where id=${id}`
    const data = await db.query(sql)
    res.send(
        formatData({
            code: data.length<0 ? 200 : 400,
            data
        })
    )
})



// --------------------------------------------------------


// GET /api/goods/roughage   获取粗粮列表
router.get('/roughage', async (req, res) => {

    // 数据库操作
    let sql = `select * from roughage`
    const data = await db.query(sql)
    res.send(formatData({data}))
})

//POST /api/goods/roughage        添加沙拉商品
router.post('/roughage', async (req, res) => {

    const {goodsname,price,imgurl,qty=10} = req.body
    let sqls = [`insert into roughage (goodsname,price,imgurl,qty) values (`]
    let sql = []
    sql.push (`'${goodsname}'`)
    sql.push (`'${price}'`)
    sql.push (`'${imgurl}'`)
    sql.push (`'${qty}'`)
    sqls = sqls + sql.join()+` )` 

    const data = await db.query(sqls)

    res.send(formatData({data}));
})

//DELETE /api/goods/salad/:id         删除沙拉商品
router.delete('/roughage/:id', async(req, res)=>{
    const {id} = req.params
    let sql = `delete  from roughage where id=${id}`
    const data = await db.query(sql)
    res.send(
        formatData({
            code: data.length<0 ? 200 : 400,
            data
        })
    )
})





// --------------------------------------------------------

// GET /api/goods/juice   获取果汁列表
router.get('/juice', async (req, res) => {

    // 数据库操作
    let sql = `select * from juice`
    const data = await db.query(sql)
    res.send(formatData({data}))
})

//POST /api/goods/roughage        添加果汁商品
router.post('/juice', async (req, res) => {

    const {goodsname,price,imgurl,qty=10} = req.body
    let sqls = [`insert into juice (goodsname,price,imgurl,qty) values (`]
    let sql = []
    sql.push (`'${goodsname}'`)
    sql.push (`'${price}'`)
    sql.push (`'${imgurl}'`)
    sql.push (`'${qty}'`)
    sqls = sqls + sql.join()+` )` 

    const data = await db.query(sqls)

    res.send(formatData({data}));
})

//DELETE /api/goods/juice/:id         删除沙拉商品
router.delete('/juice/:id', async(req, res)=>{
    const {id} = req.params
    let sql = `delete  from juice where id=${id}`
    const data = await db.query(sql)
    res.send(
        formatData({
            code: data.length<0 ? 200 : 400,
            data
        })
    )
})


// --------------------------------------------------------

// GET /api/goods/category   获取分类列表
router.get('/category', async (req, res) => {

    // 数据库操作
    let sql = `select * from category`
    const data = await db.query(sql)
    res.send(formatData({data}))
})

//POST /api/goods/add       添加分类
router.post('/add', async (req, res) => {

    const {goodsname} = req.body
    let sqls = [`insert into category (goodsname) values (`]
    let sql = []
    sql.push (`'${goodsname}'`)
    sqls = sqls + sql.join()+` )` 

    const data = await db.query(sqls)

    res.send(formatData({data}));
})




// --------------------------------------------------------

// GET /api/goods/allgoods   获取所有商品列表
router.get('/allgoods', async (req, res) => {
    const {category,id} = req.query
    //没有传参就是查询所有商品
    if(!category && !id){
        const { page = 1, qty = 10 , keyword='' } = req.query
        let index = (page - 1) * qty
        let sqls = `select * from allgoods`
        const datas = await db.query(sqls)
        let sql = [`select * from allgoods where 1=1`]
        sql.push(`and goodsname like '%${keyword}%' or category='${keyword}'`)
        sql.push(`ORDER BY time_reg desc limit ${index},${qty}`)
        sql = sql.join(' ')
        const data = await db.query(sql)
        res.send(formatData({ 
            code: data.length>0 ? 200 : 400,
            data:{
                total:datas.length,
                result:data,
                sometotal:data.length
            }}))
    }else if(category){
        // 根据传参查询对应的分类商品
        let sql =[`select * from allgoods where 1=1`]
        sql.push(`and category='${category}'`)
        sql = sql.join(' ')
        const data = await db.query(sql)
        res.send(formatData({data}))
    }else if(id){
        let sql = `select * from allgoods where id=${id}`
        const data = await db.query(sql)
        res.send(
            formatData({data})
        )
    }
})

// GET /api/goods/search   商品模糊查询
router.get('/search', async function (req, res) {
    const { keyword } = req.query
    let sql = `select * from allgoods where goodsname like '%${keyword}%' or category='${keyword}'`

    const data = await db.query(sql)
    res.send(formatData({data}))
})

//POST /api/goods/allgoods       添加商品
router.post('/allgoods', async (req, res) => {

    const {goodsname,price,qty=10,category} = req.body
    let sqls = [`insert into allgoods (goodsname,price,qty,category) values (`]
    let sql = []
    sql.push (`'${goodsname}'`)
    sql.push (`'${price}'`)
    sql.push (`'${qty}'`)
    sql.push (`'${category}'`)
    sqls = sqls + sql.join()+` )` 

    const data = await db.query(sqls)

    res.send(formatData({data}));
})



//PATCH /api/goods/updata        修改商品信息
router.patch('/updata', async(req, res)=>{
    const { id,goodsname,price,qty,category} = req.body
    let sqls = `update allgoods set `
    let sql = []
    if(goodsname){
        sql.push (`goodsname ='${goodsname}'`)
    }
    if(price){
        sql.push (`price ='${price}'`)
    }
    if(qty){
        sql.push (`qty ='${qty}'`)
    }
    if(category){
        sql.push (`category ='${category}'`)
    }
    sqls = sqls + sql.join()+` where id=${id}`
    const data = await db.query(sqls)
    res.send(
        formatData({data})
    )
})

//DELETE /api/goods/allgoods/:id         删除单个商品
router.delete('/allgoods/:id', async(req, res)=>{
    const {id} = req.params
    let sql = `delete  from allgoods where id=${id}`
    const data = await db.query(sql)
    res.send(
        formatData({data})
    )
})

